package com.service.abolishing.controller;

import com.service.abolishing.dto.ApDetailDto;
import com.service.abolishing.entity.ApDetail;
import com.service.abolishing.service.ApDetailService;
import com.service.config.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

import static com.service.config.constant.Constant.*;

/**
 * ap设备信息表(ApDetail)表控制层
 *
 * @author makejava
 * @since 2021-10-21 14:14:27
 */
@RestController
@RequestMapping("apDetail")
@Api(value = "ap设备信息接口", tags = "ap设备信息接口")
public class ApDetailController {
    /**
     * 服务对象
     */
    @Resource
    private ApDetailService apDetailService;
    @Resource
    private RedisUtil redisUtil;
    /**
     * 新增ap信息
     * @param cloudAuthToken
     * @return
     * @throws Exception
     */
    @PostMapping(value = "addApDetail",produces="application/json")
    @ApiOperation(value = "新增ap信息")
    @ResponseBody
    public Result addApDetail(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken,
                              @RequestBody ApDetailDto apDetailDto) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                ApDetail apDetail = ModelMapperUtil.strictMap(apDetailDto, ApDetail.class);
                apDetail.setId(IdUtil.getStringId());
                apDetailService.insert(apDetail);
                return Result.ok();
            }
            return Result.failure(USERNAME_Off_SITE, usernameOffSite);
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }
    /**
     * 获取ap信息
     * @param cloudAuthToken
     * @return
     * @throws Exception
     */
    @GetMapping(value = "getApDetail",produces="application/json")
    @ApiOperation(value = "获取ap信息")
    @ResponseBody
    public Result getApDetail(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                List<ApDetail> apDetailList = apDetailService.queryAll(userId);
                return Result.ok().setData(apDetailList);
            }
            return Result.failure(USERNAME_Off_SITE, usernameOffSite);
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }

    /**
     * 修改ap信息
     * @param cloudAuthToken
     * @param apDetailDto
     * @return
     * @throws Exception
     */
    @PostMapping(value = "freshApDetail",produces="application/json")
    @ApiOperation(value = "修改ap信息")
    @ResponseBody
    public Result freshApDetail(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken,
                                @RequestBody ApDetailDto apDetailDto) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                apDetailService.freshApDetail(apDetailDto);
                return Result.ok();
            }
            return Result.failure(USERNAME_Off_SITE, usernameOffSite);
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }

    /**
     * 删除ap信息
     * @param cloudAuthToken
     * @param aId
     * @return
     * @throws Exception
     */
    @GetMapping(value = "deleteApDetail",produces="application/json")
    @ApiOperation(value = "删除ap信息")
    @ResponseBody
    public Result deleteApDetail(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken,
                                 @RequestParam  @ApiParam("apId") String aId) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                apDetailService.deleteById(aId);
                return Result.ok();
            }
            return Result.failure(USERNAME_Off_SITE, usernameOffSite);
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }
    /**
     * 热力图接口
     * @param cloudAuthToken
     * @return
     * @throws Exception
     */
    @GetMapping(value = "queryData",produces="application/json")
    @ApiOperation(value = "热力图接口")
    @ResponseBody
    public Result queryData(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                List<ApDetail> apDetailList = apDetailService.queryAll(userId);
                List<ApDetailDto> apDetailDtoList = ModelMapperUtil.strictMapList(apDetailList, ApDetailDto.class);
                apDetailDtoList.forEach(apDetailDto -> {
                    Set<String> strings = redisUtil.reverseRange(apDetailDto.getManageInfoId() + ":" + apDetailDto.getId(), 0, 0);
                    if (strings.size() > 0) {
                        strings.forEach(s1 -> {
                            try {
                                ApDetailDto apDetailDto1 = JsonXMLUtils.json2obj(s1, ApDetailDto.class);
                                apDetailDto.setCount(apDetailDto1.getCount());
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        });
                    }
                });
                return Result.ok().setData(apDetailDtoList);
            }
            return Result.failure(USERNAME_Off_SITE, usernameOffSite);
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }
}
